<template>
  <div class="private_add">
    <el-card class="box-card">
      <div class="title">
        <span>添加私有人</span>
      </div>
      <div class="detail">
        <el-form :model="add" :rules="rules" ref="ruleForm" label-width="100px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="私有者" prop="private_name">
                <el-input v-model="add.private_name" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="联系电话" prop="private_tel">
                <el-input v-model="add.private_tel" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="开始时间" prop="private_start_time">
                <el-date-picker
                  v-model="add.private_start_time"
                  type="datetime"
                  placeholder="选择日期时间"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="结束时间" prop="private_end_time">
                <el-date-picker
                  v-model="add.private_end_time"
                  type="datetime"
                  placeholder="选择日期时间"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="btn">
          <el-button icon="el-icon-arrow-left" @click="back">返回</el-button>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            style="margin-left: 50px"
            >提交<i class="el-icon-upload el-icon--right"></i
          ></el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Private_Add",
  inject: ["privateRefresh"],
  data() {
    return {
      next: false,
      add: {
        private_name: "",
        private_tel: "",
        private_start_time: "",
        private_end_time: "",
      },
      rules: {
        private_name: [
          { required: true, message: "请输入私有者", trigger: "blur" },
        ],
        private_tel: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
        ],
        private_start_time: [
          { required: true, message: "请输入开始时间", trigger: "blur" },
        ],
        private_end_time: [
          { required: true, message: "请输入结束时间", trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    back() {
      this.$store.commit("maskStatus_change", false);
      this.$router.go(-1);
      this.pageRefresh();
    },
    //刷新页面
    pageRefresh() {
      this.privateRefresh();
    },
    submitForm(formName) {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          const add = this.add;
          this.axios
            .post("/private/addPrivate", add, {
              "Content-Type": "application/json",
            })
            .then((res) => {
              if (res.data.code === "200") {
                this.$message({
                  message: res.data.msg,
                  type: "success",
                });
                this.back();
              }
            })
            .catch((err) => {
              console.error(err);
              this.$message.error("添加私有者信息失败");
            });
        } else {
          this.$message({
            message: "请完整填写表单",
            type: "warning",
          });
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.box-card {
  position: absolute;
  left: 40%;
  top: 10%;
  width: 480px;
  height: 620px;
  z-index: 999;
}
.title {
  width: 90%;
  height: 5vh;
  padding: 0 0 0 30px;
  line-height: 3vh;
  text-align: left;
  font-size: 1.2rem;
  font-weight: 600;
  border-bottom: 1px dashed #c4c6c7;
}
.title span {
  display: inline-block;
  width: 80%;
  height: 55%;
  padding: 0px 20px;
  border-left: 5px solid #3b83cc;
}
.detail {
  margin: 50px auto;
  width: 80%;
}
.detail /deep/ .el-input {
  width: 85%;
}
.detail /deep/ .el-form-item__error {
  margin-left: 8%;
}
.detail /deep/ .el-form-item__label {
  margin-top: 10px;
}
.detail /deep/ .el-form-item__content {
  margin-top: 10px;
}
.btn {
  position: absolute;
  left: 50%;
  bottom: 5%;
  transform: translate(-50%, 0);
  margin-bottom: 0;
}
</style>